#include "view_heart_rate.h"
#include "Motor.h"
#include "app_trace.h"
#include "battery_ctrl.h"
#include "blecom.h"
#include "datetime.h"
#include "display_ctrl.h"
#include "display_interface_240x240.h"
#include "font/num_font.h"
#include "heart_rate_interface.h"
#include "lfs_file_manager_auto.h"
#include "platform_assert.h"
#include "rtc.h"
#include "sport_his.h"
#include "sports_mode.h"
#include "times/view_time_0.h"
#include "times/view_time_19.h"
#include "times/view_times.h"
#include "view.h"
#include "view_common.h"
#include "view_heart_rate.inc"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_heart_rate_animation(view_heart_rate_param const *param)
{
    if (param == 0 || param->file_index_0 < DISP_LFS_FILE_INDEX_BEGIN || param->file_index_0 >= DISP_LFS_FILE_INDEX_END || param->file_index_1 < DISP_LFS_FILE_INDEX_BEGIN ||
        param->file_index_1 >= DISP_LFS_FILE_INDEX_END)
    {
        return;
    }
    if (!param->is_wear)
    {
        //       _disp_show_flash_img(param->file_index_0, 25, 73, FILE_IMG_J115_notwearingawatch_25_73);
        disp_show_localize((disp_little_fs_file_index_t)param->file_index_1, localize_string_key_AlertNotWear, param->temp_index);
        return;
    }
#warning heart_rate_animation
}

static void heart_reat_background(void)
{
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 10, 34, FILE_IMG_Heart_rate_function);
}

void draw_num_with_icon_screen(int number, uint8_t num_length, uint8_t x, uint8_t y, uint8_t num_width, uint8_t num_spacing, uint8_t const *const *num_TAB, uint8_t icon_spacing,
                               uint8_t const *iocn_resource, uint8_t height_diff)
{
    uint8_t draw_x = {0};
    uint8_t draw_y = {0};
    if (num_length % 2 == 1)
    {
        draw_x = x + (num_width / 2);
        draw_y = y;
    }
    else if (num_length % 2 == 0)
    {
        draw_x = x + (num_spacing / 2);
        draw_y = y;
    }

    if (num_length == 1)
    {
        draw_x -= num_width;
        disp_draw_user_image(draw_x, draw_y, num_TAB[number]);
    }

    draw_x += (num_length - 1) * ((num_width + num_spacing) / 2);
    uint8_t i = 0;
    for (i = 0; i < num_length; i++)
    {
        disp_draw_user_image(draw_x, draw_y, num_TAB[number % 10]);
        draw_x -= (num_width + num_spacing);
        number /= 10;
    }

    draw_x += (icon_spacing + (num_width + num_spacing) * i);
    draw_y += height_diff;
    disp_draw_user_image(draw_x, draw_y, iocn_resource);
}

void view_heart_rate_display(int view)
{
#ifdef TEST_UI
    disp_ctrl.auto_refresh_time = 1000;
    disp_ctrl.auto_close_delay = 2000;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 0, 0, FILE_IMG_view_0);
#else
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.com_data.i_temp[0] = hr_get_hr_data();
        disp_ctrl.com_data.i_temp[1] = hr_if_band_wear();
        datetime_t UIdatetime = DateSync();
        disp_ctrl.com_data.i_temp[3] = UIdatetime.time.hour;
        if (if_time_hour_system_12())
        {
            disp_ctrl.com_data.i_temp[3] %= 12;
            if (disp_ctrl.com_data.i_temp[3] == 0)
            {
                disp_ctrl.com_data.i_temp[3] = 12;
            }
        }
        disp_ctrl.com_data.i_temp[4] = UIdatetime.time.minute;
        disp_ctrl.com_data.i_temp[8] = battery_ctrl_if_charging();
        disp_ctrl.com_data.i_temp[9] = battery_ctrl_get_percent();
        disp_ctrl.com_data.i_temp[5] = ble_if_connected();
    }
#endif

    heart_reat_background();
    show_time_0_ble(disp_ctrl.com_data.i_temp[6]);
    show_time_0_battery(disp_ctrl.com_data.i_temp[9]);

    uint8_t length = get_num_digit(disp_ctrl.com_data.i_temp[0]);
    if (disp_ctrl.com_data.i_temp[0] == 0 || length > 3)
    {
        disp_draw_rect_color(49, 177, 60, 178, RGB565_COLOR_WHITE);
        disp_draw_rect_color(63, 177, 74, 178, RGB565_COLOR_WHITE);
        return;
    }
    draw_num_with_icon_screen(disp_ctrl.com_data.i_temp[0], length, 49, 177, 11, 3, img_view_heart_reat_num_TAB, 3, img_view_heart_reat_bmp, 9);
}

view_event_result_t view_heart_rate_event(int view, event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    case event_view_entry:
        disp_ctrl.auto_close_delay = 60000;
        hr_test_start(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    case event_view_re_screen_on:
        disp_ctrl.auto_close_delay = 60000;
        return view_event_result_handled;
    case event_view_exit:
        hr_test_stop(HR_CTRL_TYPE_HR_TEST);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
